import { Component, OnInit,Input,Output,EventEmitter } from '@angular/core';
import {fadeIn} from "../../animate";
import { NzMessageService } from 'ng-zorro-antd';
import {TeamManagerService} from "../../my-service/teamManager.service";
import {UtilService} from "../../my-service/util.service";

@Component({
  selector: 'app-admin-setting',
  templateUrl: './admin-setting.component.html',
  styleUrls: ['./admin-setting.component.css'],
  animations:[fadeIn]
})
export class AdminSettingComponent implements OnInit {
@Output() onTeam = new EventEmitter();
@Input()  team:any;
  readImgUrl = '';
  defaultImg ='';
currentAdmin = {
  UserName:'',
  PK_UserGuid:'',
  Cover:'',
  Name:''
};
userGuid:string;
  constructor(
    private message: NzMessageService,
    private teamService:TeamManagerService,
    private utilService:UtilService,
  ) {
    this.readImgUrl = this.utilService.readImgUrl;
    this.defaultImg = this.utilService.defaultImg;
  }
  getUserGuid() {
    this.utilService.getUserInfo().then(res=>{
      this.userGuid = res.userGuid;
    })
  }
  ngOnInit() {
   this.currentAdmin.UserName= this.team.administrator;
   this.currentAdmin.PK_UserGuid= this.team.adminGuid;
   if(this.team.Cover && this.team.Cover!==this.utilService.defaultImg){
     this.currentAdmin.Cover = this.team.Cover;
   }
  }
  onClose () {
    this.team.openAdminSeeting = false;
    this.onTeam.emit(this.team);
  }
  //确认授权
  affirm () {
  if(this.currentAdmin.PK_UserGuid !== this.team.adminGuid){
    this.utilService.comfirm('您确认要更换团队管理员么？',()=>{
      let data={
        newAdministrator:this.currentAdmin.Name?this.currentAdmin.Name:this.currentAdmin.UserName,
        newAdminGuid:this.currentAdmin.PK_UserGuid,
        PK_teamGuid:this.team.teamGuid
      };
       this.teamService.modifyAdmin(data).then(res=>{
          if(res.status==1){
             if(this.currentAdmin.Cover&& this.currentAdmin.Cover!==this.utilService.defaultImg){
               this.team.Cover = this.currentAdmin.Cover;
             }
             this.team.administrator = this.currentAdmin.Name?this.currentAdmin.Name:this.currentAdmin.UserName;
             this.team.adminGuid = this.currentAdmin.PK_UserGuid;
             this.message.success('修改团队管理员成功');
             if(this.team.adminGuid!==this.userGuid){
               this.team.isAdmin = false;
               this.team.isEdit = false;
             }
             this.team.openAdminSeeting = false;
             this.onTeam.emit(this.team);
          }
       })
    });
  }else {
    this.team.openAdminSeeting = false;
    this.onTeam.emit(this.team);
  }
  }
  //选择管理员
  selectAdmin(member){
    this.currentAdmin = member;
  }
}
